package com.marvels.approve.process.controller;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.Page;
import com.marvels.approve.process.dto.FaceScreenConditionDTO;
import com.marvels.approve.process.dto.FaceSigConfigurationDTO;
import com.marvels.approve.process.dto.TimeConfHistoryDTO;
import com.marvels.approve.process.service.IFaceSigConfigurationService;
import com.marvels.approve.process.service.ITimeConfHistoryService;
import com.marvels.common.dto.MarvelsResultDTO;
import com.marvels.common.exception.MarvelsBusinessException;
import com.marvels.common.exception.MarvelsErrorCode;
import com.marvels.common.utils.MarvelsLogUtil;

/**面签参数配置
 * @author liugh
 */
@RestController
@RequestMapping("/approve/mqcspz/service")
public class FaceSigConfiguractionController {
	@Autowired
	private IFaceSigConfigurationService  faceSigConfigurationService;
	
	@Autowired
	private ITimeConfHistoryService  timeConfHistoryService;

	/***
	 * 面签参数分页查询
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("/facesigconfigPageList/{pageNo}/{pageSize}")
	@ResponseBody
	public MarvelsResultDTO getFaceSigAppointment(@PathVariable Integer pageNo,@PathVariable Integer pageSize){
		try {
			Page<TimeConfHistoryDTO> PagefaceSig = timeConfHistoryService.selectFaceSigConfiguractionPage(pageNo,pageSize);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), PagefaceSig);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn("[getFaceSigConfiguractionPage] pageNo="+pageNo+", pageSize="+pageSize+"",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("error...",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), null);
		}
	}
	
	
	/**
	 * 查询面签筛选条件
	 * @return
	 */
	@RequestMapping("faceScreenCondition")
	@ResponseBody
	public MarvelsResultDTO faceScreenCondition(){
		try {
			FaceScreenConditionDTO datas = timeConfHistoryService.getFaceScreenCondition();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>查询面签筛选条件失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>查询面签筛选条件异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	
	/**
	 * 新建面签参数明細
	 * @param faceSigConfigurationDTO
	 * @return
	 * @throws MarvelsBusinessException
	 */
	
	@RequestMapping("/addFaceSigAppointment")
	@ResponseBody
	public MarvelsResultDTO addFaceSigAppointment(@RequestBody(required=false) TimeConfHistoryDTO timeConfHistoryDto) throws MarvelsBusinessException{
		try {
			int count = faceSigConfigurationService.addFaceSigConfiguration(timeConfHistoryDto);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>>>>>>>新建面签参数明細失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>>>新建面签参数明細异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	/**
	 * 
	 * 新增面签参数和参数明細
	 * @param faceSigConfigurationDTO
	 * @return
	 * @throws MarvelsBusinessException
	 */
	@RequestMapping("/addTimeConfHistory")
	@ResponseBody
	public MarvelsResultDTO addTimeConfHistory(@RequestBody(required=false) TimeConfHistoryDTO timeConfHistoryDT) throws MarvelsBusinessException{
		try {
			int count = timeConfHistoryService.addTimeConfHistoryAndTimeConf(timeConfHistoryDT);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>>>>>>新增面签参数和参数明細失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>>新增面签参数和参数明細异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	/**
	 * 
	 * 根据版本ID获取详情
	 * @param faceSigConfigurationDTO
	 * @return
	 * @throws MarvelsBusinessException
	 */
	@RequestMapping("/getById")
	@ResponseBody
	public MarvelsResultDTO getTimeConfHistoryId(@RequestBody(required=false) TimeConfHistoryDTO timeConfHistoryDTO) throws MarvelsBusinessException{
		try {
			TimeConfHistoryDTO facesig = timeConfHistoryService.getTimeConfDetailed(timeConfHistoryDTO.getId(),timeConfHistoryDTO.getFaceSigConfigurationDTO().getFacesigId());
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), facesig);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>>>>>>根据版本ID获取详情查询失败:",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("根据版本ID获取详情查询异常:",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	/**
	 * 修改面签参数
	 * @param faceSigConfigurationDTO
	 * @return
	 * @throws MarvelsBusinessException
	 */
	@RequestMapping("/updateFaceSigAppointment")
	@ResponseBody
	public MarvelsResultDTO updateFaceSigAppointment(@RequestBody(required=false) TimeConfHistoryDTO timeConfHistoryDTO) throws MarvelsBusinessException{
		try {
			int count = timeConfHistoryService.updateFaceSigConfAndTimeconf(timeConfHistoryDTO);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), count);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>>>>>修改面签参数失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>修改面签参数异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 查询面签流程状态
	 * @return
	 */
	@RequestMapping("/getCaseNode")
	@ResponseBody
	public MarvelsResultDTO addFacesigTask(){
		try {
			List<Map<String, Object>> datas = faceSigConfigurationService.selectCaseNode();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), datas);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>>查询面签流程状态查询失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>查询面签流程状态异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}
	
	/**
	 * 查询面签流程状态
	 * @return
	 */
	@RequestMapping("/getFacesigStartTimeAndendTime")
	@ResponseBody
	public MarvelsResultDTO getFacesigStartTimeAndendTime(){
		try {
			Page<FaceSigConfigurationDTO> timeList = faceSigConfigurationService.selectStartTime();
			return MarvelsResultDTO.getResult(MarvelsErrorCode.SUCCESS.getValue(),MarvelsErrorCode.SUCCESS.getDesc(), timeList);
		} catch (MarvelsBusinessException e) {
			MarvelsLogUtil.getInstance().warn(">>>>>>>>>>查询面签流程状态失败",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.FAIL.getValue(),MarvelsErrorCode.FAIL.getDesc(), e.getMessage());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(">>>>>>>>>>查询面签流程状态异常",e);
			return MarvelsResultDTO.getResult(MarvelsErrorCode.ERROR.getValue(),MarvelsErrorCode.ERROR.getDesc(), e.getMessage());
		}
	}

	
}
